import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_border_button.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/switch/normal_switch.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';

class InventoryItemForm extends StatelessWidget {
  final VoidCallback onChangeInventory;
  final bool inventoryEnable;
  final bool canEdit;
  final TextEditingController controller;
  final FocusNode focusNode;
  final VoidCallback onAdjustInventory;
  final id;

  const InventoryItemForm(
      {Key key, this.onChangeInventory, this.inventoryEnable, this.canEdit, this.onAdjustInventory, this.controller, this.focusNode, this.id})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isEnable = FormEditStatusInheritedWidget.ofEnable(context);
    if (isEnable) {
      return Column(
        children: [
          AppSpaces.grayH6,
          if (id == null || canEdit)
            NormalFormWrapper(
              child: PressedOpacityButton(
                onPressed: onChangeInventory,
                child: NormalSwitch(
                  title: S.current.inventory_tracking,
                  // title: S.current.inventory_tracking_bom,
                  isSelect: inventoryEnable,
                ),
              ),
              hasBottomBorder: inventoryEnable ? true : false,
            ),
          if (id == null || canEdit)
            if (inventoryEnable)
              FromInputField(
                tip: S.current.initial_product_quantity,
                inputFormatterList: [
                  intInputFormatter,
                ],
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                controller: controller,
                focusNode: focusNode,
              ),
          if (id != null && inventoryEnable == true && !canEdit)
            NormalFormWrapper(
              backgroundColor: Colors.white,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  S.current.inventory_tracking,
                  // S.current.inventory_tracking_bom,
                  style: TextStyle(
                    color: aceBlackColor,
                    fontSize: getSp(16),
                    fontWeight: FontWeight.w700,
                    height: 14 / 16,
                  ),
                ),
              ),
              hasBottomBorder: true,
            ),
        ],
      );
    } else {
      if (inventoryEnable == true)
        return NormalFormWrapper(
          topPadding: getDp(10),
          bottomPadding: getDp(13),
          hasTopBorder: true,
          hasBottomBorder: false,
          child: Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      S.current.available_quantity,
                      style: TextStyle(
                        color: aceDarkGrayColor,
                        fontSize: getSp(16),
                        fontWeight: FontWeight.w400,
                        height: 16 / 14,
                      ),
                    ),
                    AppSpaces.h10,
                    Text(
                      controller.text,
                      style: TextStyle(
                        color: aceBlackColor,
                        fontSize: getSp(18),
                        fontWeight: FontWeight.w700,
                        height: 14 / 18,
                      ),
                    )
                  ],
                ),
              ),
              NormalBorderButton(
                onTap: onAdjustInventory,
                title: S.current.adjust_inventory,
              )
            ],
          ),
        );
      else
        return Container();
    }
  }
}
